package com.chinatelecom.check.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.chinatelecom.check.entity.CheckTable;
import com.chinatelecom.check.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.bean.BeanUtil;
import com.chinatelecom.framework.model.PageData;
import com.chinatelecom.framework.model.PageParam;
import com.chinatelecom.check.entity.CheckTask;
import com.chinatelecom.check.model.CheckTaskModel;
import com.chinatelecom.check.dao.CheckTaskMapper;
import com.chinatelecom.check.service.CheckTaskService;
import com.chinatelecom.framework.generic.GenericServiceImpl;

/**
 * CheckTask 量表筛查任务 服务实现
 */
@Service
@Transactional
public class CheckTaskServiceImpl extends GenericServiceImpl<CheckTask> implements CheckTaskService{
	
	@Autowired
	private CheckTaskMapper mapper;

	@Autowired
	private CheckTableService CheckTableService;

	@Autowired
	private CheckTaskDetailService CheckTaskDetailService;
	
	@Override
	public CheckTaskModel getDetail(Integer id) {
		CheckTaskModel model = new CheckTaskModel();
		CheckTask record = mapper.selectById(id);
		BeanUtil.copyProperties(record, model);
		//量表名称处理
		List<CheckTable> tableList = CheckTableService.getByIds(Arrays.asList(record.getTableIds().split(",")));
		String nameArray = tableList.stream().map(CheckTable::getName).collect(Collectors.joining(","));
		model.setTableName(nameArray);
		//测评人员处理
		model.setUserList(CheckTaskDetailService.getList(id));
		return model;
	}
	
	@Override
	public PageData<CheckTaskModel> page(PageParam param) {
		PageData<CheckTask> data = super.getPageData(param);

		List<CheckTaskModel> list = data.getList().stream().map(a -> {
			CheckTaskModel dto = new CheckTaskModel();
			BeanUtils.copyProperties(a, dto);
			return dto;
		}).collect(Collectors.toList());

		PageData<CheckTaskModel> result = new PageData<CheckTaskModel>();
		BeanUtils.copyProperties(data, result);
		result.setList(list);
		return result;
	}


}
